package com.stepshop.wechat.utils;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;

import com.alibaba.fastjson.JSON;


/**
 * 微信二维码工具包
 * @author sung
 *
 */
public class CodeUtils {
	public static String getUrlWithAccessToken(){
		String url="";
		String file = CodeUtils.class.getResource("/").getFile();
		File file2 = new File(file);
		Properties properties=new Properties();
		try {
			properties.load(new FileInputStream(file2));
			String baseUrl=properties.getProperty("url");
			String openId=properties.getProperty("openID");
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return url;
	}
	public static String getAccessToken(){
		String token="";
		String file = CodeUtils.class.getResource("/prop.properties").getFile();
		File file2 = new File(file);
		Properties properties=new Properties();
		HttpClientUtil httpClientUtil=new HttpClientUtil();
		try {
			properties.load(new FileInputStream(file2));
			String baseUrl=properties.getProperty("accessTokenUrl");
			String openId=properties.getProperty("appid");
			String secret =properties.getProperty("secret");
			String url=baseUrl+"?opengrant_type=client_credential&appid"+openId+"&secret="+secret;
			String doPost = httpClientUtil.doPost(url, "", "UTF-8");
			System.out.println(doPost);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return token;
	}
	public static void main(String[] args) {
//		HttpClientUtil clientUtil=new HttpClientUtil();
//		String doPost = clientUtil.doPost("http://discuz.comli.com/test.php","","UTF-8");
//		System.out.println(doPost);
//		String qrscene_="qrscene_555698";
//		System.out.println(qrscene_.substring(qrscene_.indexOf("_")+1, qrscene_.length()));
		Map<String,String> paramMaps=new TreeMap<String, String>();
		//
		paramMaps.put("appid", "wxd152167cedf23e16");
		//
		paramMaps.put("mch_id", "1324751901");
		//随机字符串 不长于32位  (后台生成)
		paramMaps.put("nonce_str",MD5Util.MD5Encode(new Date().getTime()+"","UTF-8") );
		//attach   附加数据，在查询API和支付通知中原样返回，该字段主要用于商户携带订单的自定义数据
		paramMaps.put("attach", "支付测试");
		//商品描述  商品或支付单简要描述
		paramMaps.put("body", "扫码支付测试");
		//商户订单号  商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
		//传参过来   
		paramMaps.put("out_trade_no", new Date().getTime()+"");
		//总金额 订单总金额，单位为分，详见支付金额
		// 前台得来   单位为分
		paramMaps.put("total_fee", "1");
		//终端IP  APP和网页支付提交用户端ip，Native支付填调用微信支付API的机器IP
		// 用户ip
		paramMaps.put("spbill_create_ip", "14.23.150.211");
		//通知地址 	接收微信支付异步通知回调地址，通知url必须为直接可访问的url，不能携带参数。
		// 是否前台传入待定
		paramMaps.put("notify_url", "http://wechat-pre.0071515.com/pangu-wechat/wechat/tenpayCallBack");
		//交易类型 JSAPI  
		paramMaps.put("trade_type", "NATIVE");
		//用户标识     可在session中获取
		paramMaps.put("openid", "o3QjlwGBjeKHvc16IR-sMO8KXvec");
		//获得签名
		String sign = Signature.getSign(paramMaps);
		paramMaps.put("sign",sign);
		//2,发送post-- https 请求 获得返回参数
		//预备发送的xml文档 
		String xmlToPost= XMLUtil.map2Xml(paramMaps,"xml");
		HttpClientUtil httpClientUtil = new HttpClientUtil();
		String url="https://api.mch.weixin.qq.com/pay/unifiedorder";
		String callBackXml = httpClientUtil.doPost(url, xmlToPost,"UTF-8");
		Map callBackMap=new HashMap();
		try {
//		解析callbackXml 
			callBackMap = XMLUtil.doXMLParse(callBackXml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String retJson = JSON.toJSONString(callBackMap);
		System.out.println(retJson);
	}
}
